package com.umeox.watch.moto.dataservice.proxy;

import android.os.IBinder;
import android.os.RemoteException;
import android.util.SparseArray;

import com.umeox.watch.moto.apiclient.model.AlbumDTO;
import com.umeox.watch.moto.apiclient.model.ChantsCountDTO;
import com.umeox.watch.moto.apiclient.model.SubmitTaskRecordDTO;
import com.umeox.watch.moto.dataservice.BuildConfig;
import com.umeox.watch.moto.dataservice.interfaces.IPullApiClient;

import net.vidageek.mirror.dsl.Mirror;

import java.util.List;

public class PullApiClientProxy implements IPullApiClient {
    private IBinder mRemote;

    private PullApiClientProxy(IBinder remote) {
        mRemote = remote;
    }

    public static IPullApiClient fetchBinder() {
        try {
            return new PullApiClientProxy((IBinder) new Mirror().on("android.os.ServiceManager").invoke().method("getService").withArgs(BuildConfig.pullApiBinderName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public IBinder asBinder() {
        return mRemote;
    }

    @Override
    public String uploadFile(String objectName, String filePath) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        String result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(objectName);
            data.writeString(filePath);
            mRemote.transact(TRANSACTION_UPLOAD_FILE, data, reply, 0);
            reply.readException();
            result = reply.readString();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public String uploadSOSFile(String filePath) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        String result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(filePath);
            mRemote.transact(TRANSACTION_UPLOAD_SOS_FILE, data, reply, 0);
            reply.readException();
            result = reply.readString();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public void uploadStep(int stepCount) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeInt(stepCount);
            mRemote.transact(TRANSACTION_UPLOAD_STEP, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public boolean uploadSOSR(String sosEid, String fileUrl) throws RemoteException{
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        boolean result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(sosEid);
            data.writeString(fileUrl);
            mRemote.transact(TRANSACTION_UPLOAD_SOSR, data, reply, 0);
            reply.readException();
            result = reply.readByte() == 1;
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public boolean uploadLocation(int locReason,byte[] data) throws RemoteException {
        android.os.Parcel _data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        boolean result;
        try {
            _data.writeInterfaceToken(DESCRIPTOR);
            _data.writeInt(locReason);
            _data.writeByteArray(data);
            mRemote.transact(TRANSACTION_UPLOAD_LOCATION, _data, reply, 0);
            reply.readException();
            result = reply.readByte() == 1;
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            _data.recycle();
        }
        return result;
    }

    @Override
    public String uploadSosLocation(byte[] data) throws RemoteException {
        android.os.Parcel _data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        String result;
        try {
            _data.writeInterfaceToken(DESCRIPTOR);
            _data.writeByteArray(data);
            mRemote.transact(TRANSACTION_UPLOAD_SOS_LOCATION, _data, reply, 0);
            reply.readException();
            result = reply.readString();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            _data.recycle();
        }
        return result;
    }

    @Override
    public boolean uploadSos(long sosId, String fileId) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        boolean result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeLong(sosId);
            data.writeString(fileId);
            mRemote.transact(TRANSACTION_UPLOAD_SOS, data, reply, 0);
            reply.readException();
            result = reply.readByte() == 1;
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public void requestSOS2Location(String eid) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(eid);
            mRemote.transact(TRANSACTION_REQUEST_SOS2, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }


    @Override
    public void uploadHeartRate(String code, long startTime, long endTime, int avgValue, int minValue, int maxValue) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(code);
            data.writeLong(startTime);
            data.writeLong(endTime);
            data.writeInt(avgValue);
            data.writeInt(minValue);
            data.writeInt(maxValue);
            mRemote.transact(TRANSACTION_UPLOAD_HEART_RATE, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public String uploadAlumFile(String filePath) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        String result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(filePath);
            mRemote.transact(TRANSACTION_UPLOAD_ALUM_FILE, data, reply, 0);
            reply.readException();
            result = reply.readString();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public void uploadALB(SparseArray<AlbumDTO> fileUrls) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeSparseArray(fileUrls);
            mRemote.transact(TRANSACTION_UPLOAD_ALB, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public boolean uploadChallengeRecord(int sportChallengeId,int sportType, String startTime, String endTime, int totalTime, int rewardValue) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        boolean result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeInt(sportChallengeId);
            data.writeInt(sportType);
            data.writeString(startTime);
            data.writeString(endTime);
            data.writeInt(totalTime);
            data.writeInt(rewardValue);
            mRemote.transact(TRANSACTION_UPLOAD_CHALLENGE_RECORD, data, reply, 0);
            reply.readException();
            result = reply.readByte() == 1;
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public boolean uploadQuranChallengeRecord(int quranLearnId, int chapterIndex, String finishTime, int duration, int rewardValue) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        boolean result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeInt(quranLearnId);
            data.writeInt(chapterIndex);
            data.writeString(finishTime);
            data.writeInt(duration);
            data.writeInt(rewardValue);
            mRemote.transact(TRANSACTION_UPLOAD_QURAN_CHALLENGE_RECORD, data, reply, 0);
            reply.readException();
            result = reply.readByte() == 1;
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public boolean uploadChants(List<ChantsCountDTO> chantsCountDTOList) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        boolean result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeTypedList(chantsCountDTOList);
            mRemote.transact(TRANSACTION_UPLOAD_CHANTS_COUNT, data, reply, 0);
            reply.readException();
            result = reply.readByte() == 1;
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public String uploadFileByFileType(String filePath, int fileType) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        String result;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(filePath);
            data.writeInt(fileType);
            mRemote.transact(TRANSACTION_UPLOAD_FILE_BY_FILE_TYPE, data, reply, 0);
            reply.readException();
            result = reply.readString();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }

    @Override
    public void submitTaskRecord(String requestId, List<SubmitTaskRecordDTO> list) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(requestId);
            data.writeTypedList(list);
            mRemote.transact(TRANSACTION_SUBMIT_TASK_RECORD, data, reply, 0);
            reply.readException();
            reply.readString();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }
}
